/*
 * =====================================================================================
 *
 *       Filename:  QCfgLog.cpp
 *
 *    Description:  this is QCfgLog's cpp file
 *
 *        Version:  1.0
 *        Created:  2012年04月18日 11时32分59秒
 *       Revision:  none
 *       Compiler:  g++
 *
 *         Author:  Michael lee
 *         E-mail:  Michael.20#qq.com
 *
 * =====================================================================================
 */
#include "QCfgLog.h"

void QCfgLog::showPath()
{
    printf("%s\n",mFilePath);
}

//build log
void QCfgLog::log(const char *format,...)
{
    time_t curTime;
    struct tm *p;
    int fd=-1;
    FILE *fp=NULL;
    va_list argList;

    fp = fopen(mFilePath,"a");
    if(NULL==fp)
    {
        fprintf(stderr,"Unable to open '%s'.\n",mFilePath);
        return;
    }
    fd = fileno(fp);
    if(flock(fd,LOCK_EX)!=0)
    {
        perror("File lock error.\n");
    }

    curTime = time(NULL);//printf time 当前时间
    p = localtime(&curTime);
    fprintf(fp,"[%i/%02i/%02i %02i:%02i:%02i]: ",1900+p->tm_year,1+p->tm_mon,p->tm_mday,\
            p->tm_hour,p->tm_min,p->tm_sec); //记录时间

    va_start(argList,format); //获取参数量
    vfprintf(fp,format,argList); //打印到文件
    va_end(argList);
    fprintf(fp,"\n");

    if(flock(fd,LOCK_UN)!=0)//解锁
    {
        perror("Ulock error.\n");
    }
    fclose(fp);
}

//set cfg val
int QCfgLog::setValue(const char *item, const char *format,...)
{
    FILE * fp;
    char buffer[Q_BUF_NUM] ;
    char *dest, *result;
    char tmpItem[Q_MAX_ITEM_LEN];
    memset(tmpItem,0,sizeof(tmpItem));
    va_list argList;
    bool hasItem=false;
    int fd=-1; 
    int ret=-1;
    strcpy(tmpItem,item);
    strcat(tmpItem,"=");
    if((fp=fopen(mFilePath, "r+")) == NULL)//not exist
    {
        printf("file is not exist,create..\n");
        if((fp=fopen(mFilePath, "w+")) == NULL)//create failed
        {
            printf("Error! fopen function setValue() Error!\n");
            return -1;
        }
    }

    fd=fileno(fp);
    ret=flock(fd,LOCK_EX); //给文件加锁
    if(ret != 0)
    {
        perror("File lock error.\n");
    }

    while(fgets(buffer,Q_BUF_NUM,fp) != NULL)
    {
        buffer[strlen(buffer)-1] = '\0'; //去掉回车符
        //printf("buffer [ %s ] \tstrlen(item) : %d\n", buffer, strlen(item));
        if (0==strncmp(item, buffer, strlen(item)))
        {
            fseek(fp,0-strlen(buffer)-1,SEEK_CUR);
            fprintf(fp,tmpItem);
            va_start(argList,format); //获取参数量
            vfprintf(fp,format,argList); //打印到文件
            va_end(argList);
            fprintf(fp,"\n");

            dest = strstr(buffer, "=")+ 1; //"="占一位 +1
            //printf("dest is [ %s ]\n", dest);  //测试是否取到正确的字符串

            if(NULL== (result=(char*)malloc(strlen(dest))))   //分配内存空间来存放结果
            {
                printf("Error!Fuction Error!malloc() at setValue()!\n");
                fclose(fp);
                return -1;
            }
            memcpy(result, dest, strlen(dest));

            ret=flock(fd,LOCK_UN); //解锁
            if(ret!=0)
            {
                perror("Ulock error.\n");
            }
            hasItem=true;
            fclose(fp);
            return 0;
        }
    }
    if(!hasItem)
    {
        //printf("build item %s\n",tmpItem);
        fseek(fp,0,SEEK_END);
        fprintf(fp,tmpItem);
        va_start(argList,format); //获取参数量
        vfprintf(fp,format,argList); //打印到文件
        va_end(argList);
        fprintf(fp,"\n");
    }
    fclose(fp);
    return 0;
}

//read cfg val
char *QCfgLog::value(const char *item)
{
    FILE * fp;
    char buffer[Q_BUF_NUM] ;
    char *dest, *result;
    int fd=-1;
    int ret=-1;
    if((fp=fopen(mFilePath, "r")) == NULL)
    {
        printf("Error! fopen function value() Error!\n");
        return NULL;
    }

    fd=fileno(fp);
    ret=flock(fd,LOCK_EX); //给文件加锁
    if(ret != 0)
    {
        perror("File lock error.\n");
    }

    while(fgets(buffer,Q_BUF_NUM,fp) != NULL)
    {
        buffer[strlen(buffer)-1] = '\0'; //去掉回车符
        //printf("buffer [ %s ] \tstrlen(item) : %d\n", buffer, strlen(item));
        if(0==strncmp(item,buffer,strlen(item)))
        {
            dest = strstr(buffer, "=")+ 1; //"="占一位 +1
          //  printf("dest is [ %s ]\n", dest);  //测试是否取到正确的字符串

            if(NULL==(result=(char*)malloc(strlen(dest))) )   //分配内存空间来存放结果
            {
                printf("Error!malloc() at value()!\n");
                fclose(fp);
                return NULL;
            }
            //printf("dest:%s len:%d len:%d\n",dest,strlen(dest),strlen(result));
			memset(result,0,strlen(dest)+1);
			memcpy(result, dest, strlen(dest));
			//printf("result:%s len:%d\n",result,strlen(result));
            ret=flock(fd,LOCK_UN); //解锁
            if(ret!=0)
            {
                perror("Ulock error.\n");
            }
            fclose(fp);
            return (result);
        }
    }
    fclose(fp);
    printf("Cant Find the Item:%s, Please Check the Item !\n",item);
    return NULL;
}
